package www.geek.filemanager.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import www.geek.filemanager.R;
import www.geek.filemanager.adapter.HomeGridViewAdapter;
import www.geek.filemanager.utils.UIHelper;
import www.geek.filemanager.customview.CircleImageView;
import www.geek.filemanager.utils.DoubleClickExitHelper;


/**
 * 程序入口
 *
 * @author : zhanggeng
 * @date   : 2015年7月24日
 * @version: v1.0
 * ----------修订历史-----------
 *
 */
public class MainActivity extends Activity implements View.OnClickListener ,
        AdapterView.OnItemClickListener{

    private Context context = this;

    private ImageView img_homepage_setting , img_homepage_user ;

    //自定义控件，用户头像
    private CircleImageView circleImageView ;

    //gridview ，承载了程序中各个界面的入口
    private GridView gv_homepage;

    //gridview adapter , provide imageview for GridView: gv_homepage
    private HomeGridViewAdapter homeGridViewAdapter = null;

    //双击退出应用
    private DoubleClickExitHelper doubleClickExitHelper = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();


    }

    public void initView() {

        //初始化——双击退出应用对象
        doubleClickExitHelper = new DoubleClickExitHelper(this);

        //初始化自定义用户圆形头像
        circleImageView = (CircleImageView) findViewById(R.id.img_homepage_userhead);
        circleImageView.setImageDrawable(getResources().getDrawable(R.mipmap.ic_launcher));
        circleImageView.setOnClickListener(this);

        //初始化设置按钮
        img_homepage_setting = (ImageView) findViewById(R.id.img_homepage_setting);
        img_homepage_setting.setOnClickListener(this);

        //初始化主页面的4个功能模块
        gv_homepage = (GridView) findViewById(R.id.gv_homepage);
        homeGridViewAdapter = new HomeGridViewAdapter(context);
        gv_homepage.setAdapter(homeGridViewAdapter);
        gv_homepage.setOnItemClickListener(this);


    }

    /**
     *  处理返回键按下事件监听
     *
     * @param keyCode  按下的返回键
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode == KeyEvent.KEYCODE_BACK){
            return doubleClickExitHelper.onKeyDown(keyCode,event);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.img_homepage_setting:
                // 进入设置界面
                UIHelper.startUserActivity(context , SettingActivity.class);
                break;
            case R.id.img_homepage_userhead:
                break;

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //点击下方条目后跳转到相应界面
        switch (position){
            case 0:
                // 进入资源管理界面
                UIHelper.startUserActivity(context , ResManagerActivity.class);
                break;
            case 1:
                // 进入应用管理界面
                UIHelper.startUserActivity(context , AppManagerActivity.class);
                break;
            case 2:
                // 进入云备份界面
                UIHelper.startUserActivity(context , CloudBackupActivity.class);
                break;
            case 3:
                // 进入云备份界面
                UIHelper.startUserActivity(context , UserCenterActivity.class);
                break;
            default:
                break;
        }
    }
}
